<?php 
namespace app\common\controller;

class Tree
{
	public function setTree($list,$fid = 'parent_id',$son = 'child',$index = 'id',$arr_obj=false)
	{
		//处理数组数据，让数组根据$index主健名倒序
		array_multisort(array_column($list, $index),SORT_DESC,$list);
		//改变多维数组的一维健名为主健名$index
		$temp = [];
		foreach ($list as $k => $v) {
			//$v[$son] = [];
			$temp[$v[$index]] = $v;
		}
		foreach ($temp as $key => &$val) {
			if($val[$fid]) {
				if(empty($temp[$val[$fid]][$son]))$temp[$val[$fid]][$son] = [];
				if(!$arr_obj)$temp[$val[$fid]][$son][$val[$index]] = $val;
				else$temp[$val[$fid]][$son][] = $val;
				unset($temp[$key]);
			}
		}
		return $temp;
	}
}